<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="项目名称">
        <el-input v-model="dataForm.projectName" placeholder="项目名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="创建日期">
         <el-col :span="11">
      <el-form-item >
        <el-date-picker type="datetime" placeholder="选择日期" v-model="dataForm.createTime"  format="yyyy-MM-dd HH:mm:ss" 
        value-format="yyyy-MM-dd HH:mm:ss" style="width: 100%;"></el-date-picker>
      </el-form-item>
    </el-col>
    <el-col class="line" :span="2">-</el-col>
    <el-col :span="11">
      <el-form-item >
        <el-date-picker type="datetime"  placeholder="选择日期" v-model="dataForm.endTime" format="yyyy-MM-dd HH:mm:ss" 
        value-format="yyyy-MM-dd HH:mm:ss"  style="width: 100%;"></el-date-picker>
      </el-form-item>
    </el-col>
        <!-- <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.createTime" style="width: 100%;"></el-date-picker> -->
      </el-form-item>
    
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <!-- <el-button v-if="isAuth('business:project:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button> -->
        <el-button v-if="isAuth('business:project:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="projectName"
        header-align="center"
        align="center"
        label="项目名称">
      </el-table-column>
      <el-table-column
        prop="projectTypeName"
        header-align="center"
        align="center"
        label="项目类型">
      </el-table-column>
      <el-table-column
        prop="projectDescription"
        header-align="center"
        align="center"
        label="项目描述">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建日期">
      </el-table-column>
      <el-table-column
        prop="projectStatus"
        header-align="center"
        align="center"
        label="项目状态" >
        <template slot-scope="scope"> 
          <span style="margin-left: 10px">
            {{projectList[scope.row.projectStatus]}}
            </span> 
          </template>
      </el-table-column>
      <el-table-column
        prop="createUserName"
        header-align="center"
        align="center"
        label="创建人">
      </el-table-column>
      <el-table-column
        prop="fileName"
        header-align="center"
        align="center"
        label="文件名">
      </el-table-column>
      <el-table-column
        prop="projectAmount"
        header-align="center"
        align="center"
        label="项目金额">
      </el-table-column>
      <el-table-column
        prop="finalProjectAmount"
        header-align="center"
        align="center"
        label="最终项目金额">
      </el-table-column>
      <el-table-column
        prop="provinceName"
        header-align="center"
        align="center"
        label="省">
      </el-table-column>
      <el-table-column
        prop="cityName"
        header-align="center"
        align="center"
        label="市">
      </el-table-column>
      <el-table-column
        prop="districtName"
        header-align="center"
        align="center"
        label="区">
      </el-table-column>
       <el-table-column
        prop="address"
        header-align="center"
        align="center"
        label="详细地址">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.projectId)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.projectId)">删除</el-button>
          <el-button type="text" size="small" @click="downloadHandle(scope.row.projectId)">下载</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageNum"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update> -->
  </div>
</template>

<script>
  export default {
    data () {
      return {
        dataForm: {
          projectName: ''
        },
        dataList: [],
        pageNum: 1,
        pageSize: 10,
        total: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        projectList:{
            '0':'立项',
            '1':'正在进行中',
            '2':'归档'
      },
     optionsList:[]   
      }
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/business/project/list'),
          method: 'get',
          params: this.$http.adornParams({
            'pageNum': this.pageNum,
            'pageSize': this.pageSize,
            'projectName': this.dataForm.projectName,
            'createTime':this.dataForm.createTime,
            'endTime':this.dataForm.endTime
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.total = data.page.total
          } else {
            this.dataList = []
            this.total = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageNum = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageNum = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
          var map = {};
          map.id=id
        this.$router.push({name : "business-projectaddorupdate",params:{info : map}});
        // this.addOrUpdateVisible = true
        // this.$nextTick(() => {
        //   this.$refs.addOrUpdate.init(id)
        // })
      },
      // chain(){
      //   this.$http({
      //     url: this.$http.adornUrl('business/china/listAll'),
      //     method: 'get',
      //     params: this.$http.adornParams()
      //   }).then(({data}) => {
      //      if (data && data.code === 0) {
      //         // console.log(JSON.stringify(data.page))
      //        this.optionsList =data.page
      //       //  console.log(JSON.stringify(this.optionsList))
      //     } else {
      //       this.optionsList = []
      //     }  
      //   })
      // },

       fromtYN(row,column,cellValue){
         console.log(JSON.stringify(cellValue))
          if(cellValue==='0'){
            return "立项"
          } 
          if(cellValue==='1'){
            return "正在进行中"
          } 
          if(cellValue==='2'){
            return "归档"
          }
          return ""
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.projectId
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/business/project/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      //下载文件
      downloadHandle (id) {
        console.log(id)
        //此方法根据自己项目来的,getFileInfoByUid是项目接口,根据项目自行调整
        this.$http({
          url: this.$http.adornUrl('/business/project/get-down-load-path'),
          method: 'get',
          params: this.$http.adornParams(
            {
              'id': id
            }
          )
        }).then(({data})=>{
        //res就是上图的返回值,然后调用下面的download方法
        console.log(JSON.stringify(data))
        this.download(data.data.name,data.data.originalPath)
        })   
      },
      // getFileInfoByUid (id){
          
      // },
      download(name,url){
        const ele = document.createElement('a');
        // ele.setAttribute('href', url);
        //   //this.$options.filters['filterUrl']是调用全局过滤器,filterUrl是你自己项目main.js里面定义的过滤器
        // ele.setAttribute('download',name);
        // ele.style.display = 'none';
        // document.body.appendChild(ele);
        // ele.click();
        // document.body.removeChild(ele);

         // 这里是将url转成blob地址，
        fetch(url).then(res => res.blob()).then(blob => { // 将链接地址字符内容转变成blob地址
          ele.href = URL.createObjectURL(blob)
          console.log(ele.href)
          ele.download = ''
          document.body.appendChild(ele)
          ele.click()
        })
      }
    }
  }
</script>
